package sanshui.system.trade.factory.timeManager.type;

import lombok.extern.slf4j.Slf4j;
import sanshui.system.trade.factory.timeManager.TimeManager;
import sanshui.system.trade.util.CommonTimeUtil;

import java.time.LocalDateTime;

/**
 * 实时交易时间管理器
 *
 * */
@Slf4j
public class RtTimeManager extends TimeManager {


    public RtTimeManager(String frequency, LocalDateTime startTime, LocalDateTime endTime) {
        super(frequency, startTime, endTime);
    }

    /**
     * 实时系统，对于下一次的时间，如果历史时间用完以后，会根据已有频率生成时间进行驱动
     * */
    @Override
    public boolean hasNext() {
        // 回测没有时间戳，按照频率制造时间戳
        log.info("--------------------------------------");
        LocalDateTime next = this.generateNextTime();
        if (null != next) loadTimestamp(next);
        return true;
    }

    private LocalDateTime generateNextTime(){
        if (null == tryNextTime()){
            LocalDateTime timestampCurrentTemp = CommonTimeUtil.timestampAdd(timestampCurrent, frequency);
            LocalDateTime now = LocalDateTime.now();
            log.info("生成下次驱动时间，计划驱动时间:{}，当前时间:{}", CommonTimeUtil.getNowTime16(timestampCurrentTemp), CommonTimeUtil.getNowTime16(now));
            if (now.compareTo(timestampCurrentTemp) >= 0){
                // 保证系统时间大于驱动时间，避免出现未来时间
                loadTimestamp(timestampCurrentTemp);
            }
        }
        return null;
    }
}
